package com.suyang.syt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.GroupDao;
import com.suyang.syt.dao.interfaces.UserGroupDao;
import com.suyang.syt.model.Group;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.GroupDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.GroupService;
import com.suyang.syt.util.EmptyUtil;

@Service
public class GroupServiceImpl implements GroupService {
	@Autowired
	private GroupDao groupDao;
	@Autowired
	private UserGroupDao userGroupDao;

	@Override
	public AjaxObj add(Group group) throws Exception {
		if (group != null && EmptyUtil.isNotEmpty(group.getName())) {
			if (groupDao.countName(group.getName()) > 0) {
				return new AjaxObj(0, "已经存在该组名，请重新添加！");
			}
			groupDao.add(group);
			return new AjaxObj(1, "增加成功！");
		}
		return new AjaxObj(0, "增加失败！");
	}

	@Override
	public AjaxObj deletes(String groupIds) throws Exception {
		if (EmptyUtil.isNotEmpty(groupIds)) {
			if (userGroupDao.countUserRole(groupIds) > 0) {
				return new AjaxObj(0, "要删除的角色中还有用户，不能删除！");
			}
			groupDao.deletes(groupIds);
			return new AjaxObj(1, "删除角色成功！");
		}
		return new AjaxObj(0, "请选择要删除的记录！");
	}

	@Override
	public AjaxObj update(GroupDTO groupDTO) throws Exception {
		if (groupDTO != null && EmptyUtil.isNotEmpty(groupDTO.getName())) {
			Group group = groupDao.load(groupDTO.getId());
			if (group != null) {
				if (!groupDTO.getName().equals(group.getName()) && groupDao.countName(groupDTO.getName()) > 0) {
					return new AjaxObj(0, "已经存在该组名，请重新添加！");
				}
				group.setModifyName(groupDTO.getModifyName());
				group.setModifyTime(groupDTO.getModifyTime());
				group.setDescribes(groupDTO.getDescribes());
				group.setName(groupDTO.getName());
				groupDao.update(group);
				return new AjaxObj(1, "更新成功！");
			}
		}
		return new AjaxObj(0, "更新失败！");
	}

	@Override
	public PagerEasyUI<Group> listByEasyUI(int page, int rows, String sort, String order, String name) throws Exception {
		StringBuilder hql = new StringBuilder(" from Group g where g.name != '超级管理员' ");
		if (EmptyUtil.isNotEmpty(name)) {
			hql.append(" and g.name like '%").append(name).append("%' ");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by g.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<Group> easyUIPager = new PagerEasyUI<Group>();
		List<Group> groups = groupDao.findByEasyUI(hql.toString(), page, rows);
		easyUIPager.setRows(groups);
		easyUIPager.setTotal((Long) groupDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public AjaxObj deleteClearUser(String groupIds) throws Exception {
		if (EmptyUtil.isNotEmpty(groupIds)) {
			userGroupDao.clearUsers(groupIds);
			return new AjaxObj(1, "成功清除了该组中的用户，可以删除该组了！");
		}
		return new AjaxObj(0, "清空用户失败，请稍后再试！");
	}

	@Override
	public List<Group> listGroup() throws Exception {
		List<Group> groups = groupDao.listGroup();
		groups.add(0, new Group(-1, "所有用户"));
		return groups;
	}

}
